<template>
    <div class="components_edit_modal">
        <el-dialog :title="edited?'院系修改':'院系添加'" :visible.sync="state" :show-close="true">
            <el-form :model="secondSchoolParams"   :rules="rules" ref="secondSchoolParams" label-position="right" :close-on-click-modal="false">

                        <el-form-item label="院系名称"  prop="secondSchoolName">
                                <el-input v-model.trim="secondSchoolParams.secondSchoolName" placeholder="请输入院系名称"></el-input>
                             </el-form-item>
                        
                            <el-form-item label="所属院校" >
                                <el-input  :placeholder="schoolName" disabled="disabled"></el-input>
                             </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addSeconSchool('secondSchoolName')">确 定</el-button>
                <el-button size="small" @click="state = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    export default {
        data() {
            return {
                 state: false,
                 rules: {
                    secondSchoolName: [
                        { required: true, message: '院系名称不能为空', trigger: 'blur',type:"string"},
                    ],
                 },
                secondSchoolParams: {
                    secondSchoolName:null,
                    schoolId:null
                },
                schoolName:null,
                id:null,
                edited:false
            }
        },
        created() {
            
        },
        mounted() {},
        methods: {
            // 新建专业
            addSeconSchool(secondSchoolName) {
                let vm = this;
                
                this.$refs['secondSchoolParams'].validate((valid) => {
                    if (valid) {
                        let url = api.saveSecondSchool;
                        let shcoolParamsN = vm.secondSchoolParams;
                        
                         if(vm.edited){
                            url = api.editSecondSchool;
                            shcoolParamsN.id = vm.id;
                        }
                        // if(schoolParamsN.secondSchoolName.trim().length < 1){
                        //     vm.$message.info("请输入正确的院系名称");
                        //     return;
                        // }
                        // console.log(JSON.stringify(shcoolParamsN));
                        this.axios.post(url,shcoolParamsN)
                        .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    type: 'success',
                                    message: response.data.msg,
                                });
                                this.$emit('refreshList');
                                vm.state = false;
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                    } else {
                        return false;
                    }
                });
                
            },
            showDialog(id,name){
                if(this.$refs['secondSchoolParams']!==undefined){
                    this.$refs['secondSchoolParams'].resetFields();
                }
                this.schoolName = name;
                this.secondSchoolParams.schoolId = id;
                this.state = true;
                this.edited = false;
                this.secondSchoolParams.secondSchoolName = null;
                // console.log(JSON.stringify(row));
            },
            editSecondSchoolDetail(row,schoolName){
                if(this.$refs['secondSchoolParams']!==undefined){
                    this.$refs['secondSchoolParams'].resetFields();
                }
                console.log(JSON.stringify(row)+"-->"+schoolName)
                this.state = true;
                this.edited = true;
                this.secondSchoolParams={
                    secondSchoolName:row.secondSchoolName,
                    schoolId:row.schoolId
                };
                this.schoolName=schoolName;
                this.id = row.id;
            }
        },
        components: {}
    };
</script>
<style scoped>

</style>